<template>
    <div>
           <el-card class="box-cord">
            <div slot="header" class="clearfix">
                <span>账户信息</span>
            </div>
             <!-- gutter:设置col与col之间的间距
             span:设置每个col占据的宽度，总宽度为24，24是权单位，不是像素和cm厘米
             -->
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form ref="editFormRef" :model="editForm" >
                        <el-form-item label="用户名" >
                            <el-input v-model="editForm.name"></el-input>
                        </el-form-item>
                        <el-form-item label="手机号码">
                            <el-input v-model="editForm.mobile" disabled=""> </el-input>
                        </el-form-item>
                        <el-form-item label="简介" >
                            <el-input type="textear" v-model="editForm.intro"></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱" >
                            <el-input v-model="editForm.email"></el-input>
                        </el-form-item>
                        <el-form-item  >
                            <el-button type='primary' @click="editAccount()">更新账户</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
                <el-col :span="4">
                    <!-- <img :src="editForm.photo" alt="" width="400px" height="400px" style=" border-radius: 50%"> -->
                    <el-upload
                      action=""
                      :http-request="httpRequest"
                      :show-file-list="false"

                     >
                      <img v-if="editForm.photo" :src="editForm.photo" class="avatar">
                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>

                </el-col>
            </el-row>

        </el-card>
    </div>
</template>

<script>
export default {
  name: 'AcountCom',
  created () {
    this.getAccountInfo() // 获取频道信息
  },
  methods: {
    // 用户头像处理  更新用户头像,自定义头像上传

    httpRequest (rst) {
      // 通过 fd收集上传的附件信息
      var fd = new FormData()
      fd.append('photo', rst.file)
      // axios发起请求,同时携带附件
      var pro = this.$http.patch(`/user/photo`, fd)
      pro
        .then(result => {
          this.editForm.photo = result.data.data.photo

          this.$store.commit('upPhoto', result.data.data.photo)
        })
        .catch(err => {
          return this.$message.error('更新账户头像失败' + err)
        })
    },
    // 修改用户信息存储入库

    editAccount () {
      this.$refs.editFormRef.validate(valid => {
        // axios.post(地址, 请求体数据, 请求字符串)

        var pro = this.$http.patch(`/user/profile`, this.editForm)
        pro
          .then(result => {
            this.$store.commit('upName', result.data.data.name)

            this.$message.success('修改账户成功!')
            // 导航到 文章列表页面
          })
          .catch(err => {
            return this.$message.error('修改账户信息成功' + err)
          })
      })
    },
    getAccountInfo () {
      var pro = this.$http.get(`/user/profile`)
      pro.then(result => {
        this.editForm = result.data.data
      })
        .catch(err => {
          return this.$message.error('获取用户信息错误' + err)
        })
    }
  },
  data () {
    return {
      editForm: {
        name: '',
        intro: '',
        email: '',
        mobile: '',
        photo: []
      }
    }
  }

}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<style lang='less'>

</style>
